//
//  Fraction.m
//  Home-2 - 完成分数练习
//
//  Created by 岁变 on 16/7/5.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction

- (instancetype)initWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator {
    
    self = [super init];
    
    if (self) {
        
        _denominator = denominator;
        
        _numerator = numerator;
        
    }
    
    return self;
    
}


+(instancetype)FractionWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator {
    
    Fraction *fra = [[[Fraction alloc] initWithNumerator:numerator denominator:denominator] autorelease];
    
    return fra;
    
}

#pragma mark - 加法
- (Fraction *)addWithFraction:(Fraction *)fra {
    
    NSInteger newNum = self.numerator * fra.denominator + self.denominator * fra.numerator;
    
    NSInteger newDe = self.denominator * fra.denominator;
    
    NSInteger number = 0;
    
    //约分
    
    if (newNum >= newDe) {
        
        for (NSInteger i = newDe; i > 0; i--) {
            
            if (newNum % i == 0 && newDe % i == 0) {
                
                number = i;
                
                break;
                
            }
            
        }
        
    }
    else {
        
        for ( NSInteger i = newNum; i > 0; i--) {
            
            if (newDe % i == 0 && newNum % i == 0) {
                
                number = i;
                
                break;
                
            }
        }
        
    }
    
    
    Fraction *newFra = [Fraction FractionWithNumerator:newNum / number denominator:newDe / number];
    
    return newFra;
    
}

#pragma mark - 减法
- (Fraction *)deWithFraction:(Fraction *)fra {
    
    NSInteger newNum = self.numerator * fra.denominator - self.denominator * fra.numerator;
    
    NSInteger newDe = self.denominator * fra.denominator;
    
    if (newNum < 0) {
        
        NSInteger new = newNum * (-1);
        
        NSInteger number = 0;
        
        if (new  >= newDe) {
            
            for (NSInteger i = newDe; i > 0; i--) {
                
                if (new % i == 0 && newDe % i == 0) {
                    
                    number = i;
                    
                    break;
                    
                }
                
            }
            
        }
        else {
            
            for ( NSInteger i = new; i > 0; i--) {
                
                if (newDe % i == 0 && new % i == 0) {
                    
                    number = i;
                    
                    break;
                    
                }
            }
            
        }
        
        
        Fraction *newFra = [Fraction FractionWithNumerator:new / number * (-1) denominator:newDe / number];
        
        return newFra;
        
    }
    
    else {
        
        NSInteger number = 0;
        
        if (newNum >= newDe) {
            
            for (NSInteger i = newDe; i > 0; i--) {
                
                if (newNum % i == 0 && newDe % i == 0) {
                    
                    number = i;
                    
                    break;
                    
                }
                
            }
            
        }
        else {
            
            for ( NSInteger i = newNum; i > 0; i--) {
                
                if (newDe % i == 0 && newNum % i == 0) {
                    
                    number = i;
                    
                    break;
                    
                }
            }
            
        }
        
        
        Fraction *newFra = [Fraction FractionWithNumerator:newNum / number denominator:newDe / number];
        
        return newFra;
        
    }
    
}


#pragma mark - 乘法

- (Fraction *)mulWithFraction:(Fraction *)fra {
    
    NSInteger newNum = self.numerator *fra.numerator;
    
    NSInteger newDe = self.denominator * fra.denominator;
    
    NSInteger number = 0;
    
    if (newNum >= newDe) {
        
        for (NSInteger i = newDe; i > 0; i--) {
            
            if (newNum % i == 0 && newDe % i == 0) {
                
                number = i;
                
                break;
                
            }
            
        }
        
    }
    else {
        
        for ( NSInteger i = newNum; i > 0; i--) {
            
            if (newDe % i == 0 && newNum % i == 0) {
                
                number = i;
                
                break;
                
            }
        }
        
    }
    
    
    Fraction *newFra = [Fraction FractionWithNumerator:newNum / number denominator:newDe / number];
    
    return newFra;
    
}

#pragma mark - 除法

- (Fraction *)divWithFraction:(Fraction *)fra {
    
    
    
    NSInteger newNum = self.numerator *fra.denominator;
    
    NSInteger newDe = self.denominator * fra.numerator;
    
    NSInteger number = 0;
    
    if (newNum >= newDe) {
        
        for (NSInteger i = newDe; i > 0; i--) {
            
            if (newNum % i == 0 && newDe % i == 0) {
                
                number = i;
                
                break;
                
            }
            
        }
        
    }
    else {
        
        for ( NSInteger i = newNum; i > 0; i--) {
            
            if (newDe % i == 0 && newNum % i == 0) {
                
                number = i;
                
                break;
                
            }
        }
        
    }
    
    
    Fraction *newFra = [Fraction FractionWithNumerator:newNum / number denominator:newDe / number];
    
    return newFra;
}

#pragma mark - 比较大小

- (void)compareWithFraction:(Fraction *)fra {
    
    NSInteger number = self.numerator * fra.denominator - self.denominator * fra.numerator;
    
    if (number > 0) {
        
        NSLog(@"前者大于后者");
    }
    
    else if (number < 0) {
        
        NSLog(@"前者小于后者");
    }
    
    else {
        
        NSLog(@"相等");
    }

}



@end
